package com.example.concurrency.threadpool;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author ZhenWuWang
 */
public class MyThreadPoolExecutor extends ThreadPoolExecutor
{
    public MyThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit,
                                BlockingQueue<Runnable> workQueue, RejectedExecutionHandler handler)
    {
        super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, handler);
    }

    @Override
    protected void afterExecute(Runnable r, Throwable t)
    {
        super.afterExecute(r, t);
        if (r instanceof MyRunnable)
        {
            String errmsg = "";
            if (t != null)
            {
                errmsg = " Throwable: " + t.toString();
            }
            MyRunnable myRunnable = (MyRunnable) r;
            System.out.println(myRunnable.getName() + " afterExecute" + " " + errmsg);
        }
        System.out.println(Thread.currentThread().getName() + "afterExecute");
    }

    @Override
    protected void beforeExecute(Thread t, Runnable r)
    {
        System.out.println(Thread.currentThread().getName() + "beforeExecute");
        super.beforeExecute(t, r);
        if (r instanceof MyRunnable)
        {
            String errmsg = "";
            if (t != null)
            {
                errmsg = " Throwable: " + t.toString();
            }
            MyRunnable myRunnable = (MyRunnable) r;
            System.out.println(myRunnable.getName() + " beforeExecute" + " " + errmsg);
        }
    }

    @Override
    protected void terminated()
    {
        super.terminated();
        System.out.println("terminated");
    }
}
